package com.backweb.Controller;


import com.backweb.model.Article;
import com.backweb.service.IArticle;
import com.backweb.tools.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {


  @Autowired
  private IArticle iArticle;

    @GetMapping("/total")
    public int articletotal( @RequestParam(value="day",required=false,defaultValue="0")Integer day  ){


        int total=0;
        try
        { total=iArticle.dayTotal(day);}
        catch (Exception ex){}
        return  total;
    }






    @GetMapping("/list")
    public ResultMap articleList(
            @RequestParam(value="t1",required=false,defaultValue="")String t1,
            @RequestParam(value="t2",required=false,defaultValue="")String t2,
            @RequestParam(value="pageNo",required=false,defaultValue="1")Integer pageNo,
            @RequestParam(value="rows",required=false,defaultValue="10")Integer rows,
            @RequestParam(value="type",required=false,defaultValue="0")Integer type,
            @RequestParam(value="day",required=false,defaultValue="0")Integer day
    )
    {
        Map map = new HashMap();
          int total=0;
          List<Article> items=new ArrayList<>();
          try {

              if(type==0) {
                  items = iArticle.findPage(t1, t2, pageNo, rows);
                  total = iArticle.findPageTotal(t1, t2);
              }else
              {
                  items=iArticle.dayPage(day,pageNo, rows);
                  total=iArticle.dayTotal(day);

              }
          }catch (Exception ex)  { }
        map.put("total", total);
        map.put("items", items);
        return  ResultMap.success(map);
    }

    @GetMapping("/del")
    public ResultMap articledel(  @RequestParam(value="rid")Integer rid )
    {

        try {
            Article article = iArticle.findArticleByRid(rid);
            if (article != null) {
                iArticle.delete(article);
            }
            return  ResultMap.success();
        }catch (Exception ex)
        {
            return ResultMap.failed();

        }


    }
}
